package Rozetka.Pages;

import Rozetka.ReportWriter.ReportWriter;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import java.util.NoSuchElementException;

public abstract class BasePage {

    public static WebDriver driver;
    protected static String baseUrl = "http://rozetka.com.ua/";

    private static By loginPopup = By.xpath("//*[@id='user_menu']/div/a");
    private static By toRegistraion = By.xpath("/html/body/header/div/div/div/div[3]/div[3]/div/div/div/form/div[5]/a");
    private static By searchInputField = By.name("text");
    private static By searchButton = By.className("btn-link-i");

    // menu items
    private static By menuNote = By.xpath(".//*[@id='computers-notebooks']/div/a");
    private static By menuNoteTitle = By.xpath("/html/body/div/div[3]/div/div/div/div/div/div[2]/div/h1");

    private static By menuPhone = By.xpath(".//*[@id='phones-mp3-gps']/div/a");
    private static By menuPhoneTitle = By.xpath("/html/body/div/div[3]/div/div/div/div/div/div[2]/div/h1");

    private static By menuAudio = By.xpath(".//*[@id='tv-photo-video']/div/a");
    private static By menuAudioTitle = By.xpath("/html/body/div/div[3]/div/div/div/div/div/div[2]/div/h1");

    private static By menuBT = By.xpath(".//*[@id='bt']/div/a");
    private static By menuBtTitle = By.xpath("/html/body/div/div[4]/div/div/div/div/div/div[2]/div/h1");

    private static By menuOutdoorsman = By.xpath(".//*[@id='bt']/div/a");
    private static By menuOutdoorsmanTitle = By.xpath("/html/body/div/div[4]/div/div/div/div/div/div/div/h1");

    private static By menuChild = By.xpath(".//*[@id='kids']/div/a");
    private static By menuChildTitle = By.xpath("/html/body/div/div[4]/div/div/div/div/div/div");

    private static By menuHouseGarden = By.xpath(".//*[@id='house_garden']/div/a");
    private static By menuHouseGardenTitle =By.xpath("/html/body/div/div[4]/div/div/div/div/div/div/div/h1");

    private static By menuAuto = By.xpath(".//*[@id='273290']/div/a");
    private static By menuAutoTitle = By.xpath("/html/body/div/div[3]/div/div/div/div/div/div/div/h1");

    private static By menuTickets = By.xpath(".//*[@id='fatmenu_14']/div/a");
    private static By menuTicketsTitle =By.xpath("/html/body/div/div[3]/div/div/div/div/ul/li/a/span");

    //logout link
    private static By logOutLink = By.name("signout");

    //basket enabled
    private static By openBasket = By.name("open_cart");
    private static By deleteBasketItem = By.name("delete");


    public BasePage(WebDriver driver) {
        this.driver = driver;
    }

    public void open(){
        driver.get(baseUrl);
        ReportWriter.info("Open Home Page");
    }

    public RegistrationPage openRegistrationPage() {
        ReportWriter.info("Go to registration page");
        driver.findElement(loginPopup).click();
        driver.findElement(toRegistraion).click();
        return new RegistrationPage(driver);
    }

    public SearchPage searchProduct (String productSearchQuery) {
        ReportWriter.info("Start Search");



        try {
            driver.findElement(searchInputField).sendKeys(productSearchQuery);
            //Сделана дополнительная проверка ибо в хроме иногда плейсхолдер оставался
            if (driver.findElement(searchInputField).getText() != productSearchQuery){
                driver.findElement(searchInputField).clear();
                driver.findElement(searchInputField).sendKeys(productSearchQuery);
            }
            driver.findElement(searchButton).click();
        } catch (NoSuchElementException e) {
            ReportWriter.error("Didn't find element for search");
        }

        return new SearchPage(driver);
    }

    public boolean checkMenuNote() {
        ReportWriter.info("Check menu Note");
        driver.findElement(menuNote).click();
        if (driver.findElement(menuNoteTitle).isDisplayed()) {
            ReportWriter.info("Menu Note is present");
            return true;
        } else {
            ReportWriter.error("Menu Note is not present");
            return false;
        }
    }

    public boolean checkMenuPhone() {
        ReportWriter.info("Check menu Phone");
        driver.findElement(menuPhone).click();
        if (driver.findElement(menuPhoneTitle).isDisplayed()) {
            ReportWriter.info("Menu Phone is present");
            return true;
        } else {
            ReportWriter.error("Menu Phone is not present");
            return false;
        }
    }

    public boolean checkMenuAudio() {
        ReportWriter.info("Check menu Audio");
        driver.findElement(menuAudio).click();
        if (driver.findElement(menuAudioTitle).isDisplayed()) {
            ReportWriter.info("Menu Audio is present");
            return true;
        } else {
            ReportWriter.error("Menu Audio is not present");
            return false;
        }
    }

    public boolean checkMenuBT() {
        ReportWriter.info("Check menu BT");
        driver.findElement(menuBT).click();
        if (driver.findElement(menuBtTitle).isDisplayed()) {
            ReportWriter.info("Menu BT is present");
            return true;
        } else {
            ReportWriter.error("Menu BT is not present");
            return false;
        }
    }

    public boolean checkMenuOutdoorsman() {
        ReportWriter.info("Check menu Outdoorsman");
        driver.findElement(menuOutdoorsman).click();
        if (driver.findElement(menuOutdoorsmanTitle).isDisplayed()) {
            ReportWriter.info("Menu Outdoorsman is present");
            return true;
        } else {
            ReportWriter.error("Menu Outdoorsman is not present");
            return false;
        }
    }

    public boolean checkMenuChild() {
        ReportWriter.info("Check menu Child");
        driver.findElement(menuChild).click();
        if (driver.findElement(menuChildTitle).isDisplayed()) {
            ReportWriter.info("Menu Child is present");
            return true;
        } else {
            ReportWriter.error("Menu Child is not present");
            return false;
        }
    }

    public boolean checkMenuHomeAndGarden() {
        ReportWriter.info("Check menu Home And Garden");
        driver.findElement(menuHouseGarden).click();
        if (driver.findElement(menuHouseGardenTitle).isDisplayed()) {
            ReportWriter.info("Menu Home And Garden is present");
            return true;
        } else {
            ReportWriter.error("Menu Home And Garden is not present");
            return false;
        }
    }

    public boolean checkMenuAuto() {
        ReportWriter.info("Check menu Auto");
        driver.findElement(menuAuto).click();
        if (driver.findElement(menuAutoTitle).isDisplayed()) {
            ReportWriter.info("Menu Auto is present");
            return true;
        } else {
            ReportWriter.error("Menu Auto is not present");
            return false;
        }
    }

    public boolean checkMenuTickets() {
        ReportWriter.info("Check menu Tickets");
        driver.findElement(menuTickets).click();
        if (driver.findElement(menuTicketsTitle).isDisplayed()) {
            ReportWriter.info("Menu Tickets is present");
            return true;
        } else {
            ReportWriter.error("Menu Tickets is not present");
            return false;
        }
    }


    public boolean logOut() {
        try {
            driver.findElement(logOutLink).click();
            return true;
        } catch (NoSuchElementException e) {
            ReportWriter.error("No LogOut link. Maybe is already registered");
            return false;
        }
    }

    public boolean cleanBasket() {
        try {
            driver.findElement(openBasket).click();
            driver.findElement(deleteBasketItem).click();
            return true;
        } catch (NoSuchElementException e) {
            ReportWriter.error("Didn't find element for cleaning Basket");
            return false;
        }
    }
}
